package com.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DatePicker {
	/**
	 * 获取月度显示的第一天，注意不一定是当月的第一天
	 * @param year  -- 年 如：2012
	 * @param month -- 月 如：2
	 * @return
	 */
	public static Date getFirstDate(int year,int month){
		Calendar c = Calendar.getInstance();
		c.set(year, month - 1,1, 0,0, 0);
		c.set(Calendar.MILLISECOND, 0);
		int week = c.get(Calendar.DAY_OF_WEEK);
		if(week == Calendar.MONDAY){
			return c.getTime();
		}
		long tmp = 0;
		if(week == Calendar.SUNDAY){
			tmp = c.getTime().getTime() - 6*1000L*60*60*24;
		}else{
			tmp = c.getTime().getTime() - (week - 2)*1000L*60*60*24;
		}
		return new Date(tmp);
	}
	/**
	 * 获取月度显示的最后一天，注意不一定是当月的最后一天
	 * @param year  -- 年 如：2012
	 * @param month -- 月 如：2
	 * @return
	 */
	public static Date getLastDate(int year,int month){
		Calendar c = Calendar.getInstance();
		c.set(year, month - 1,1, 23,59, 59);
		c.set(Calendar.MILLISECOND, 999);
		
		int maxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		c.set(Calendar.DAY_OF_MONTH, maxDay);
		
		int week = c.get(Calendar.DAY_OF_WEEK);		
		if(week == Calendar.SUNDAY) return c.getTime();
		long d = c.getTime().getTime() + (8 - week)*1000L*60*60*24;
		
		return new Date(d);
	}
	/**
	 * 获取一个月的第一天
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date firstDayOfMonth(int year,int month){
		Calendar cal = Calendar.getInstance();
		cal.set(year, month - 1, 1,0,0,0);
		cal.set(Calendar.MILLISECOND, 0);
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		String date = sdf.format(cal.getTime());
		date += " 00:00:00";
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			return cal.getTime();
			//e.printStackTrace();
		}
	}
	/**
	 * 获取一个月的最后一天
	 * @param year
	 * @param month
	 * @return
	 */
	public static Date maxDayOfMonth(int year,int month){
		Calendar cal = Calendar.getInstance();
		cal.set(year, month - 1, 1,23,59,59);
		int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(year, month - 1, maxDay);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}
	
	/**
	 * 获取本周显示的第一天
	 * @return
	 */
	public static Date getFirstDateOfWeek(){
		Calendar c = Calendar.getInstance();
		//Calendar.HOUR_OF_DAY24小时制，Calendar.HOUR12小时制
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		int week = c.get(Calendar.DAY_OF_WEEK);
		if(week == Calendar.MONDAY){
			return c.getTime();
		}
		long tmp = 0;
		if(week == Calendar.SUNDAY){
			tmp = c.getTime().getTime() - 6*1000L*60*60*24;
		}else{
			tmp = c.getTime().getTime() - (week - 2)*1000L*60*60*24;
		}
		return new Date(tmp);
	}
	
	/**
	 * 获取本周显示的最后一天
	 * @return
	 */
	public static Date getLastDateOfWeek(){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		int week = c.get(Calendar.DAY_OF_WEEK);
		if(week == Calendar.SUNDAY){
			return c.getTime();
		}
		long d = c.getTime().getTime() + (8 - week)*1000L*60*60*24;
		
		return new Date(d);
	}
	
	/**
	 * 获取本周显示的第一天
	 * @return
	 */
	public static Date getFirstDateOfWeek(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		//Calendar.HOUR_OF_DAY24小时制，Calendar.HOUR12小时制
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		int week = c.get(Calendar.DAY_OF_WEEK);
		if(week == Calendar.MONDAY){
			return c.getTime();
		}
		long tmp = 0;
		if(week == Calendar.SUNDAY){
			tmp = c.getTime().getTime() - 6*1000L*60*60*24;
		}else{
			tmp = c.getTime().getTime() - (week - 2)*1000L*60*60*24;
		}
		return new Date(tmp);
	}
	
	/**
	 * 获取本周显示的最后一天
	 * @return
	 */
	public static Date getLastDateOfWeek(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		int week = c.get(Calendar.DAY_OF_WEEK);
		if(week == Calendar.SUNDAY){
			return c.getTime();
		}
		long d = c.getTime().getTime() + (8 - week)*1000L*60*60*24;
		
		return new Date(d);
	}
	
	/**
	 * 获取本周显示的第一天
	 * @param date  时间
	 * @param  firstDay  1-7  1表示周一，7表示周日
	 * @return
	 */
	public static Date getFirstDateOfWeek(Date date,int firstDay){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		//Calendar.HOUR_OF_DAY24小时制，Calendar.HOUR12小时制
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		//当前日期的索引
		int week = c.get(Calendar.DAY_OF_WEEK);
		//开始日期的索引
		int firstweek = 0;
		if(firstDay == 7){
			firstweek = 1;
		}else{
			firstweek = firstDay + 1;
		}
		//计算出第一天与日期天的差值
		if(firstweek < week){
			c.add(Calendar.DATE, firstweek - week);
		}else if(firstweek > week){
			c.add(Calendar.DATE, firstweek - week - 7);
		}
		return c.getTime();
	}
	
	/**
	 * 获取本周显示的最后一天
	 * @param date  时间
	 * @param  lastDay  1-7  1表示周一，7表示周日
	 * @return
	 */
	public static Date getLastDateOfWeek(Date date,int lastDay){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		//当前日期的索引
		int week = c.get(Calendar.DAY_OF_WEEK);
		//开始日期的索引
		int firstweek = 0;
		if(lastDay == 7){
			firstweek = 1;
		}else{
			firstweek = lastDay + 1;
		}
		//计算出第一天与日期天的差值
		if(firstweek < week){
			c.add(Calendar.DATE, firstweek - week + 6);
		}else if(firstweek > week){
			c.add(Calendar.DATE, firstweek - week - 7 + 6);
		}else{
			c.add(Calendar.DATE, 6);
		}
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		return c.getTime();
	}
	
	 public static void main(String[] args) {
    	try {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms");
        	SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        	//System.out.println(sdf.format(maxDayOfMonth(2017, 6))+"=="+maxDayOfMonth(2017, 6).getTime());
        	//System.out.println(sdf.format(getFirstDateOfWeek()));
        	for (int i = 1; i < 8; i++) {
        		System.out.println(sdf.format(getLastDateOfWeek(sdf1.parse("2017-06-19"),i))+"==="+i);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	 /**
	  * 获取日期的前/后天数的日期
	  * @param sDate
	  * @param days
	  * @return
	  */
	public static Date dateAddDay(Date sDate, int days) {
		// 将开始时间赋给日历实例
		Calendar c = Calendar.getInstance();
		c.setTime(sDate);
		// 计算结束时间
		c.add(Calendar.DATE, days);
		return c.getTime();
	}
	
	/**
	 * 获取某时间的本季度的第一天
	 * @return
	 * */
	public static Date getCurrentQuarterStartTime(Date date) { 
        Calendar c = Calendar.getInstance(); 
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1; 
        try { 
            if (currentMonth >= 1 && currentMonth <= 3) 
                c.set(Calendar.MONTH, 0); 
            else if (currentMonth >= 4 && currentMonth <= 6) 
                c.set(Calendar.MONTH, 3); 
            else if (currentMonth >= 7 && currentMonth <= 9) 
                c.set(Calendar.MONTH, 4); 
            else if (currentMonth >= 10 && currentMonth <= 12) 
                c.set(Calendar.MONTH, 9); 
            c.set(Calendar.DATE, 1); 
            c.set(Calendar.HOUR_OF_DAY, 0);
    		c.set(Calendar.MINUTE, 0);
    		c.set(Calendar.SECOND, 0);
    		c.set(Calendar.MILLISECOND, 0);
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return c.getTime(); 
    } 
	 /** 
     * 获取某季度的结束时间，即2012-03-31
     * 
     * @return 
     */ 
    public static  Date getCurrentQuarterEndTime(Date date) { 
        Calendar c = Calendar.getInstance(); 
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1; 
        try { 
            if (currentMonth >= 1 && currentMonth <= 3) { 
                c.set(Calendar.MONTH, 2); 
                c.set(Calendar.DATE, 31); 
            } else if (currentMonth >= 4 && currentMonth <= 6) { 
                c.set(Calendar.MONTH, 5); 
                c.set(Calendar.DATE, 30); 
            } else if (currentMonth >= 7 && currentMonth <= 9) { 
                c.set(Calendar.MONTH,8); 
                c.set(Calendar.DATE, 30); 
            } else if (currentMonth >= 10 && currentMonth <= 12) { 
                c.set(Calendar.MONTH, 11); 
                c.set(Calendar.DATE, 31); 
            } 
            c.set(Calendar.HOUR_OF_DAY, 23);
    		c.set(Calendar.MINUTE, 59);
    		c.set(Calendar.SECOND, 59);
    		c.set(Calendar.MILLISECOND, 999);
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
        return c.getTime(); 
    }
    
}
